package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.MyLikesAdapter;
import com.dingwei.superiordiary.adapter.adapter.MyPostsAdapter;
import com.dingwei.superiordiary.bean.MyLikesBean;
import com.dingwei.superiordiary.bean.MyhistoryPostsBean;
import com.dingwei.superiordiary.mvp.presenter.MyLikesPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 南柯一梦丶
 * Created time 2018/10/22.
 * Personal signature 余生还长 何必慌张
 * Page description :
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
public class MyLikesActivity extends BaseActivity implements Contract.MyLikesView {

    MyLikesPresenter myLikesPresenter = new MyLikesPresenter(this);

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;


    private MyLikesAdapter adapter;
    //    private RecyclerTouchListener onTouchListener;
    private int page = 1;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_likes;
    }

    @Override
    protected void initData() {
        page = 1;
        myLikesPresenter.initData(page, true, false);
    }

    @Override
    protected void initView() {
        tvTitle.setText("我赞过的");


        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        adapter = new MyLikesAdapter(this);
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                myLikesPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                myLikesPresenter.initData(page, true, false);
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                MyLikesBean.DataBean.ListBean bean = adapter.getItem(i);
                if (bean.transpond_type.equals("0")) {
                    intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                    intent.putExtra("id", bean.id);
                    startActivity(intent);
                } else {
                    intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                    intent.putExtra("id", bean.transpond_content.id);
                    startActivity(intent);
                }
            }
        });
    }

    public void showData() {
        if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
            noDataLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(MyLikesBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData();
    }

    @Override
    public void onLoadMore(MyLikesBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData();
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
